package com.micro.service.gateway.util;

import com.micro.service.common.base.DateTool;
import org.apache.commons.lang.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class CookieTool {

    private final static String juuid = "juuid";

    private final static String domain = "lvxiucai.club";

    /**
     * 获得浏览器JSESSIONID
     *
     * @param request
     * @param response
     * @return
     */
    public static String getOrSetUserSessionId(HttpServletRequest request, HttpServletResponse response) {
        String session_id = null;
        Cookie cookie = CookieTool.getCookieByName(request, juuid);
        if (cookie != null) {
            session_id = cookie.getValue();
        } else {
            //生成唯一ID
            String uid = SessionIdKit.me().generate(request);
            //获取当天结束时间
            CookieTool.addCookie(request, response, juuid, uid, DateTool.calcTodayEndSecond(),domain);
            session_id = uid;
        }

        return session_id;
    }


    public static Cookie getCookieByName(HttpServletRequest request, String name) {
        Cookie cookie = null;
        Cookie[] cookies = request.getCookies();
        if ((cookies != null) && (cookies.length > 0)) {
            for (Cookie c : cookies) {
                if (c.getName().equals(name)) {
                    cookie = c;
                    break;
                }
            }
        }
        return cookie;
    }

    public static String getValueByName(HttpServletRequest request, String name) {
        Cookie cookie = null;
        Cookie[] cookies = request.getCookies();
        if ((cookies != null) && (cookies.length > 0)) {
            for (Cookie c : cookies) {
                if (c.getName().equals(name)) {
                    cookie = c;
                    break;
                }
            }
        }
        return ((cookie != null) ? cookie.getValue() : "");
    }

    public static Cookie addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value,
                                   Integer expiry, String domain) {
        Cookie cookie = new Cookie(name, value);

        if (expiry != null) {
            cookie.setMaxAge(expiry.intValue());
        }
        if (StringUtils.isNotBlank(domain)) {
            String domainUrl = getDomain(request);
            String [] domainStr = domain.split(",");
            for (String str : domainStr) {
                if (domainUrl.indexOf(str) != -1) {
                    cookie.setDomain(str);
                }
            }
        }
        String ctx = request.getContextPath();
        cookie.setPath((StringUtils.isBlank(ctx)) ? "/" : ctx);
        response.addCookie(cookie);
        return cookie;
    }

    public static Cookie addCookie(HttpServletRequest request, HttpServletResponse response, String name, String value,
                                   String domain) {
        Cookie cookie = new Cookie(name, value);

        if (StringUtils.isNotBlank(domain)) {
            cookie.setDomain(domain);
        }
        String ctx = request.getContextPath();
        cookie.setPath((StringUtils.isBlank(ctx)) ? "/" : ctx);
        response.addCookie(cookie);
        return cookie;
    }

    public static void cancleCookie(HttpServletRequest request, HttpServletResponse response, String name,
                                    String domain) {
        Cookie cookie = new Cookie(name, "");

        cookie.setMaxAge(0);
        String ctx = request.getContextPath();
        cookie.setPath((StringUtils.isBlank(ctx)) ? "/" : ctx);
        if (StringUtils.isNotBlank(domain)) {
            String domainUrl = getDomain(request);
            String [] domainStr = domain.split(",");
            for (String str : domainStr) {
                if (domainUrl.indexOf(str) != -1) {
                    cookie.setDomain(str);
                }
            }
        }
        response.addCookie(cookie);
    }

    /**
     * 获取域名
     * @param request
     * @return
     */
    public static String getDomain(HttpServletRequest request) {
        StringBuffer url = request.getRequestURL();
        String tempContextUrl = url.delete(url.length() - request.getRequestURI().length(), url.length()).append("/").toString();
        return tempContextUrl;
    }
}
